from flask import Flask, render_template, request
import pandas as pd

app = Flask("网页展示excel表格")


def read_excel():
  """用pandas读取excel"""
  path = "数据-学生成绩表.xlsx"
  return pd.read_excel(path)


@app.route("/", methods=['POST', 'GET'])
def index():
  table = ""
  if request.method == 'POST':
    df = read_excel()
    sname = request.form.get("sname")
    if sname:
      df = df.query(f"姓名 == '{sname}'")
    table = df.to_html(
      classes='table table-hover table-bordered',
      index=False)

  # alt enter
  return render_template(
    "index.html", table=table
  )


app.run(debug=True)
